import _ from 'lodash'
export function mergeSources(data) {
    const cloneData = _.cloneDeep(data)
    var oldDataRule = [];
    cloneData.forEach((item) => {
        if (item) {
            item.isTime = item.timestampInterval ? true : false
            var oldObj = {
                ...item,
                siddhiAppName: item.siddhiAppName,
                attributeConfList: [],
                isTime: item.timestampInterval ? true : false //是否打开高级配置 (时间戳有值就打开)
            };
            oldObj.attributeConfList.push(item);
            oldDataRule.push(oldObj);
        }

    });
    // 此处代码有问题--保留
    var newData = [];
    var newObj = {};
    oldDataRule.forEach((el, i) => {
        if (!newObj[el.siddhiAppName]) {
            newData.push(el);
            newObj[el.siddhiAppName] = true;
        } else {
            newData.forEach((el) => {
                if (el.siddhiAppName === oldDataRule[i].siddhiAppName && el.simulationType === oldDataRule[i].simulationType) {
                    el.attributeConfList = el.attributeConfList.concat(
                        oldDataRule[i].attributeConfList
                    );
                }
            });
        }
    });
    console.log(newData, '三种类型 数据组装', oldDataRule)
    return oldDataRule
}
export function setAttrTypeVal(attributesType, attrType) {
    // 正则
    if (attributesType == 'REGEX_BASED') {
        let defaultValue = ''
        const boolRegex = "(?i)(true|false)";
        const stringRegex = "[A-Z]([a-z]){4}";
        const intRegex = "[0-9]{3}";
        const longRegex = "-?[0-9]{1,19}";
        const floatRegex = "[+-]?([0-9]*[.])?[0-9]+";
        const doubleRegex = "[0-9]{1,13}(\\.[0-9]*)?";
        switch (attrType) {
            case "BOOL":
                defaultValue = boolRegex;
                break;
            case "STRING":
                defaultValue = stringRegex;
                break;
            case "INT":
                defaultValue = intRegex;
                break;
            case "LONG":
                defaultValue = longRegex;
                break;
            case "FLOAT":
                defaultValue = floatRegex;
                break;
            case "DOUBLE":
                defaultValue = doubleRegex;
                break;
        }
        return defaultValue
    }
    // 时间类型
    if (attributesType == 'CUSTOM_DATA_BASED') {
        let defaultValue = ''
        switch (attrType) {
            case "BOOL":
                defaultValue = "FALSE";
                break;
            case "STRING":
                defaultValue = "Data" + Math.floor(Math.random() * (100 - 1 + 1)) + 1;
                break;
            case "INT":
                defaultValue = Math.floor(Math.random() * (100 - 1 + 1)) + 1;
                break;
            case "LONG":
                defaultValue = Math.floor(Math.random() * (100 - 1 + 1)) + 1;
                break;
            case "FLOAT":
            case "DOUBLE":
                defaultValue = Math.random() * (100 - 0) + 0;
                break;
        }
        return defaultValue
    }


}
export function mergeSourcesForm(data) {
    const cloneData = _.cloneDeep(data)
    let sources = [];
    cloneData.forEach((item) => {

        item.attributeConfList.forEach((ite) => {
            sources.push({
                ...item,
                siddhiAppName: item.siddhiAppName,
                simulationType: item.simulationType,
                streamName: ite.name || ite.streamName,
                timestampInterval: ite.timestampInterval,
                // 随机类型需要这个字段
                attributeConfiguration: item.simulationType == 'RANDOM_DATA_SIMULATION' && ite.listAttributes.map((m) => {
                    if (m.list) {
                        m.list = [m.list];
                    }
                    return {
                        ...m,
                        type: m.attributesType,
                    };
                }),
            });
        });
    });
    return sources
}